<?php 

namespace App\Bmob;


class BmobApi 
{

	private $client;
	private $tableName;
	public function __construct($tableName)
	{
		$this->client = new \GuzzleHttp\Client(['base_uri' => 'https://api.bmob.cn/1/',
			'headers'=>[
				'X-Bmob-Application-Id'=>env('BMOB_APP_ID'),
				'X-Bmob-REST-API-Key'=>env('BMOB_REST_KEY'),
				'Content-Type'=>'application/json'
			],
			'verify' => false

		]);
		$this->tableName=$tableName;
	}


	private function init()
	{

	}


	public function create()
	{

	}


	

	public function batchCreate($data)
	{
		$body=$this->createBatchBody($data,$this->tableName);
		$response = $this->client->request('POST', '/1/batch', [
    		'body' => json_encode($body)
		]);
		if($response->getStatusCode()==200)
		{
			return $response->getBody()->getContents();
		}
		return false;
		
	}

	private function createBatchBody($data,$tableName)
	{

		$requests=[];
		foreach ($data as $key => $value) {
			# code...
			/**
				 {
            "method": "POST",
            "path": "/1/classes/TableName",
            "body": {
              key1: value1,
              key2: value2,
              ...
            }
          },
			*/	

			$item=['method'=>'POST','path'=>'/1/classes/'.$tableName,'body'=>$value];
			$requests[]=$item;
		}
		return ['requests'=>$requests];

	}

}